package com.cjj.refresh;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

import ohos.global.resource.NotExistException;

import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;
import ohos.media.image.common.Size;

import java.io.IOException;
import java.io.InputStream;

/**
 * Created by cjj on 2015/8/5.
 * 绘制贝塞尔来绘制波浪形
 */
public class WaveView extends Component implements Component.DrawTask {
    private int waveHeight;
    private int headHeight;
    private PixelMap pixelMapDis; // 位图
    private PixelMapHolder pixelMapHolder;
    private Path path;
    private Paint paint;

    public WaveView(Context context) {
        this(context, null, 0);
    }

    public WaveView(Context context, AttrSet attrs) {
        this(context, attrs, 0);
    }

    public WaveView(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        path = new Path();
        paint = new Paint();
        paint.setColor(new Color(Color.argb(150, 43, 43, 43)));
        paint.setAntiAlias(true);
        // 获取位图
        pixelMapDis = getPixelMap(ResourceTable.Media_gg);
        pixelMapHolder = new PixelMapHolder(pixelMapDis);
        addDrawTask(this);
    }

    public int getHeadHeight() {
        return headHeight;
    }

    public void setHeadHeight(int headHeight) {
        this.headHeight = headHeight;
    }

    public int getWaveHeight() {
        return waveHeight;
    }

    public void setWaveHeight(int waveHeight) {
        this.waveHeight = waveHeight;
    }

    /**
     * getPixelMap
     *
     * @param resId resId
     * @return PixelMap
     */
    public PixelMap getPixelMap(int resId) {
        InputStream drawableInputStream = null;
        try {
            drawableInputStream = getResourceManager().getResource(resId);
            ImageSource.SourceOptions sourceOptions = new ImageSource.SourceOptions();
            sourceOptions.formatHint = "image/png";
            ImageSource imageSource = ImageSource.create(drawableInputStream, null);
            ImageSource.DecodingOptions decodingOptions = new ImageSource.DecodingOptions();
            decodingOptions.desiredSize = new Size(0, 0);
            decodingOptions.desiredRegion = new ohos.media.image.common.Rect(0, 0, 0, 0);
            decodingOptions.desiredPixelFormat = PixelFormat.ARGB_8888;
            return imageSource.createPixelmap(decodingOptions);
        } catch (IOException | NotExistException e) {
            e.printStackTrace();
        } finally {
            if (drawableInputStream != null) {
                try {
                    drawableInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return pixelMapDis;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        RectFloat rectF = new RectFloat(0, 0, getEstimatedWidth(), 400);
        RectFloat rectF1 = new RectFloat(0, 0,
                pixelMapDis.getImageInfo().size.width, pixelMapDis.getImageInfo().size.height);
        canvas.drawPixelMapHolderRect(pixelMapHolder, rectF1, rectF, paint);
        // 重置画笔
        path.reset();
        path.lineTo(0, headHeight);
        // 绘制贝塞尔曲线
        path.quadTo((float) ((double) getEstimatedWidth() / 2),
                headHeight + waveHeight, getEstimatedWidth(), headHeight);
        path.lineTo(getEstimatedWidth(), 0);
        canvas.drawPath(path, paint);
    }

    @Override
    public void addDrawTask(DrawTask task) {
        super.addDrawTask(task);
        onDraw(this, mCanvasForTaskOverContent);
    }
}